package web

import (
	"github.com/gin-gonic/gin"
	"mallgo/internal/common/response"
	"mallgo/utils"
	"strconv"
	"time"
)

func state(ctx *gin.Context) {
	ctx.JSON(200, response.Success("server is running"))
}

func now(ctx *gin.Context) {
	date := time.Now()
	ctx.JSON(200, response.Success(date.Format("2006-01-02 15:04:05")))
}

func password(ctx *gin.Context) {
	num, err := strconv.Atoi(ctx.PostForm("len"))
	if err != nil {
		num = 1
	}
	ctx.JSON(200, response.Success(utils.HashPassword(ctx.PostForm("password"), ctx.PostForm("salt"), num)))
}

func salt(ctx *gin.Context) {
	num, _ := strconv.Atoi(ctx.Query("len"))
	salt, _ := utils.GenerateSalt(num)
	ctx.JSON(200, response.Success(salt))
}

func sha256(ctx *gin.Context) {
	num := ctx.PostForm("data")
	salt := utils.Sha256(num)
	ctx.JSON(200, response.Success(salt))
}

func token(ctx *gin.Context) {
	token := ctx.Query("token")
	if token == "" {
		ctx.JSON(200, response.Error(400, "token is empty"))
		return
	}
	str, _ := utils.GenerateToken(token)
	ctx.JSON(200, response.Success(str))
}
